﻿using CommandProcessor.Command;
using Core.Common;
using Data.Infrastructure;
using Data.Repositories;
using Domain.Commands;
using Domain.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.Models;

namespace Domain.Handlers
{
    //Dung de check add
    public class CanAddMaterialFormula : IValidationHandler<CreateOrUpdateMaterialFormulaCommand>
    {
        private readonly IMaterialFormulaRepository MaterialFormulaRepository;
        public CanAddMaterialFormula(IMaterialFormulaRepository MaterialFormulaRepository, IUnitOfWork unitOfWork)
        {
            this.MaterialFormulaRepository = MaterialFormulaRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateMaterialFormulaCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.Name == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateMaterialFormulaCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateMaterialFormulaHandler : ICommandHandler<CreateOrUpdateMaterialFormulaCommand>
    {
        private readonly IMaterialFormulaRepository MaterialFormulaRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateMaterialFormulaHandler(IMaterialFormulaRepository MaterialFormulaRepository, IUnitOfWork unitOfWork)
        {
            this.MaterialFormulaRepository = MaterialFormulaRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateMaterialFormulaCommand command)
        {
            var MaterialFormula = new MaterialFormula
            {
                ID = command.ID,
                Name = command.Name,
                Code = command.Code,
                UOMID = command.UOMID,
                DesignDetailID=command.DesignDetailID,
                MaterialTypeID=command.MaterialTypeID,
                ProductModelID=command.ProductModelID,
                Quantity=command.Quantity,
            };
            if (MaterialFormula.ID == 0)
                MaterialFormulaRepository.Add(MaterialFormula);
            else
                MaterialFormulaRepository.Update(MaterialFormula);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateMaterialFormulaCommand> commands)
        {
            List<MaterialFormula> list = new List<MaterialFormula>();
            foreach (var item in commands)
            {
                var company = new MaterialFormula
                {
                    ID = item.ID,
                    Code=item.Code,
                    DesignDetailID=item.DesignDetailID,
                    MaterialTypeID=item.MaterialTypeID,
                    Name=item.Name,
                    ProductModelID=item.ProductModelID,
                    Quantity=item.Quantity,
                    UOMID=item.UOMID
                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                MaterialFormulaRepository.AddRange(list.Where(x => x.ID == 0));
            else
                MaterialFormulaRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateMaterialFormulaCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            MaterialFormulaRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteMaterialFormulaHandler : ICommandHandler<DeleteMaterialFormulaCommand>
    {
        private readonly IMaterialFormulaRepository MaterialFormulaRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteMaterialFormulaHandler(IMaterialFormulaRepository MaterialFormulaRepository, IUnitOfWork unitOfWork)
        {
            this.MaterialFormulaRepository = MaterialFormulaRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteMaterialFormulaCommand command)
        {
            var MaterialFormula = MaterialFormulaRepository.GetById(command.ID);
            MaterialFormulaRepository.Delete(MaterialFormula);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteMaterialFormulaCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            MaterialFormulaRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteMaterialFormulaCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}

